package com.ruoyi.web.controller.cems;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cems.domain.BmsWorkChecklists;
import com.ruoyi.cems.service.IBmsWorkChecklistsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cems.domain.BmsWorkChecklistsDetail;
import com.ruoyi.cems.service.IBmsWorkChecklistsDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作检查单明细Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@Api(description = "工作检查单明细")
@RestController
@RequestMapping("/cems/workCheckListsDetail")
public class BmsWorkChecklistsDetailController extends BaseController
{
    @Autowired
    private IBmsWorkChecklistsDetailService bmsWorkChecklistsDetailService;

    @Autowired
    private IBmsWorkChecklistsService bmsWorkChecklistsService;

    /**
     * 查询工作检查单明细列表
     */
    @ApiOperation(value = "查询工作检查单明细列表")
    @GetMapping("/list")
    public TableDataInfo list(BmsWorkChecklistsDetail bmsWorkChecklistsDetail)
    {
        startPage();
        List<BmsWorkChecklistsDetail> list = bmsWorkChecklistsDetailService.selectBmsWorkChecklistsDetailList(bmsWorkChecklistsDetail);
        return getDataTable(list);
    }

    /**
     * 导出工作检查单明细列表
     */
    @ApiOperation(value = "导出工作检查单明细列表")
    @Log(title = "工作检查单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmsWorkChecklistsDetail bmsWorkChecklistsDetail)
    {
        List<BmsWorkChecklistsDetail> list = bmsWorkChecklistsDetailService.selectBmsWorkChecklistsDetailList(bmsWorkChecklistsDetail);
        ExcelUtil<BmsWorkChecklistsDetail> util = new ExcelUtil<BmsWorkChecklistsDetail>(BmsWorkChecklistsDetail.class);
        util.exportExcel(response, list, "工作检查单明细数据");
    }

    /**
     * 获取工作检查单明细详细信息
     */
    @ApiOperation(value = "获取工作检查单明细详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(bmsWorkChecklistsDetailService.selectBmsWorkChecklistsDetailByID(ID));
    }

    /**
     * 新增工作检查单明细
     */
    @Log(title = "工作检查单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsWorkChecklistsDetail bmsWorkChecklistsDetail)
    {
        bmsWorkChecklistsDetail.setID(UUID.randomUUID().toString().replace("-", ""));
        bmsWorkChecklistsDetail.setCreatedBy(getUserId().toString());
        bmsWorkChecklistsDetail.setCreatedTime(new Date());
        return toAjax(bmsWorkChecklistsDetailService.insertBmsWorkChecklistsDetail(bmsWorkChecklistsDetail));
    }

    /**
     * 修改工作检查单明细
     */
    @ApiOperation(value = "修改工作检查单明细")
    @Log(title = "工作检查单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsWorkChecklistsDetail bmsWorkChecklistsDetail)
    {
        BmsWorkChecklists bmsWorkChecklists = bmsWorkChecklistsService.selectBmsWorkChecklistsByID(bmsWorkChecklistsDetail.getCheckId());
        String checkPeopleId = bmsWorkChecklists.getCheckPeopleId();
        Long userId = getUserId();
        if (checkPeopleId != null && userId != null) {
            // 将 Long 类型的 userId 转换为 String 类型
            String userIdStr = userId.toString();
            if (checkPeopleId.equals(userIdStr)) {
                bmsWorkChecklistsDetail.setUpdatedBy(userIdStr);
                bmsWorkChecklistsDetail.setUpdatedTime(new Date());
                bmsWorkChecklistsDetail.setFillPeople(userIdStr);
                bmsWorkChecklistsDetail.setFileTime(new Date());
                return toAjax(bmsWorkChecklistsDetailService.updateBmsWorkChecklistsDetail(bmsWorkChecklistsDetail));
            } else {
                // 处理不相等的情况，例如抛出异常或返回错误信息
                throw new IllegalStateException("任务执行人和当前用户不匹配");
            }
        } else {
            // 处理空值的情况
            throw new IllegalStateException("检查人ID或当前用户ID为空");
        }
    }

    /**
     * 删除工作检查单明细
     */
    @ApiOperation(value = "删除工作检查单明细")
    @Log(title = "工作检查单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(bmsWorkChecklistsDetailService.deleteBmsWorkChecklistsDetailByIDs(IDs));
    }
}
